// components/rank-item/index.js
import { getSongDetail } from '../../service/api_player'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    rankData: {
      type: Object,
      value: {}
    },
    updateText: {
      type: String,
      value: '每天更新'
    },
    backgroundColor: {
      type: String,
      value: 'orange'
    },
    titleColor: {
      type: String,
      value: 'black'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    partData: [],
    titlePicURL: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleClickRankItem() {
      wx.navigateTo({
        url: '/pages/song-menu-list/index?id=' + this.data.rankData.id,
      })
    }
  },
  // 监听数据传入
  observers: {
    'rankData': function(val) {
      if(val.length == 0) return
      // 拿出三首作为列表
      this.setData({ partData: val.tracks.slice(0, 3)})

      // 获取数据的第一首歌的封面
      const id = this.properties.rankData.tracks[0].id
      getSongDetail(id).then(res => {
        this.setData({ titlePicURL: res.songs[0].al.picUrl })
      })
    }
  }

})
